//@author A0100088R
//
// **********
//
// filter class receives the currently display tasks
// from logic, and sieves out the tasks that
// user does not wish to see
//
// **********

#include "Filter.h"

Filter::Filter(void){

}

Filter::~Filter(void){

}

vector<Task> Filter::execute(string command, vector<Task> list) {

	ofstream writeFile;
	writeFile.open(FILENAME_LOGGER.c_str(), ofstream::out | ofstream::app);
	writeFile << "Attempting to filter" << endl;
	writeFile.close();


	if(list.size() == NIL) {
		throw exception("Nothing to filter from");
	}
	//filter status
	if(command == DONE || command == WORK_IN_PROGRESS || command == CMI || command == WIP || command == ARCHIVE) {//filter by status
		if(command == WORK_IN_PROGRESS) {
			command = WIP;
		}
		for(vector<Task>::iterator iter = list.begin(); iter != list.end(); iter++){
			if((*iter).getStatus() != command){
				modifiedList.push_back(*iter);//only display the tasks that have not been filtered 
			}
		}
	
	//filter priority
	} else if(command == HIGH || command == MEDIUM || command == LOW || command[0] == SYMBOL_PRIORITY){//filter by priority
		if(command[0] == SYMBOL_PRIORITY){
			int temp = command.find_first_not_of(WHITE_SPACE,1);
			command = command.substr(temp);
		}

		for(vector<Task>::iterator iter = list.begin(); iter != list.end(); iter++){
			if((*iter).getPriority() != command){
				modifiedList.push_back(*iter);//only display the tasks that have not been filtered
			}
		}
	
	//filter date
	} else if(command.size() >= 3 && (command[1] ==  SLASH || command[2] == SLASH)) {
	
		int find = command.find(SLASH);
		string day = command.substr(0,find);
		string month = command.substr(find+1);

		int intDay = 0;
		int intMonth = 0;
		intDay = atoi(day.c_str());
		intMonth = atoi(month.c_str());

		if(intDay <= 0 || intDay > 31) {
			throw exception ("Invalid date to filter");
		}

		if(intMonth <= 0 || intMonth > 12) {
			throw exception ("Invalid date to filter");
		}
	

		for(vector<Task>::iterator iter = list.begin(); iter != list.end(); iter++){
			if((*iter).getDeadline() != command){
				modifiedList.push_back(*iter);
			}
		}
	//filter those with time
	} else if(command == TIMED) {
		for(vector<Task>::iterator iter = list.begin(); iter != list.end(); iter++){
			if((*iter).getTime() == SPACE) {
				modifiedList.push_back(*iter);
			}
		}

	//filter those without time
	} else if( command == TIMELESS) {
		for(vector<Task>::iterator iter = list.begin(); iter != list.end(); iter++){
			if((*iter).getTime() != SPACE){
				modifiedList.push_back(*iter);
			}
		}
	//filter those without dates
	} else if(command == DATELESS) {
		for(vector<Task>::iterator iter = list.begin(); iter != list.end(); iter++){
			if((*iter).getDeadline() != EMPTY){
				modifiedList.push_back(*iter);
			}
		}

	//filter those with dates
	} else if (command == DATED) {
		for(vector<Task>::iterator iter = list.begin(); iter != list.end(); iter++){
			if((*iter).getDeadline() == EMPTY){
				modifiedList.push_back(*iter);
			}
		}



	}else {
		throw exception("Invalid filter command");
	}

	return modifiedList;
}